/**
 * 
 */
package com.swg.cocobox.client;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.LinkedList;
import java.util.List;

import com.swg.cocobox.KeyChange;

/**
 * @author satriaprayoga
 *
 */
public abstract class Client extends Thread{
	
	protected Selector selector;
	
	protected List<KeyChange> keyChanges;

	public Client() throws IOException{
		keyChanges=new LinkedList<KeyChange>();
		selector=initSelector();
	}
	
	public abstract void startClient();
	
	protected void finishConnection(SelectionKey key)throws IOException{
		SocketChannel channel=(SocketChannel)key.channel();
		try{
			channel.finishConnect();
		}catch(IOException e){
			key.cancel();
			e.printStackTrace();
			return ;
		}
		key.interestOps(SelectionKey.OP_WRITE);
	}
	
	private Selector initSelector()throws IOException{
		return SelectorProvider.provider().openSelector();
	}
}
